package com.itboye.mengwu.app.ui.mymeng;

import java.util.Observable;
import java.util.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.OrderDetailBean;
import com.itboye.mengwu.data.presenter.OrderPresenter;

public class ActivityTuiKuan extends BaseActivity implements OnClickListener, Observer {
	TextView txt_title;
	ImageView back, goods_im, yishoudao, meishoudao;
	TextView goods_name, goods_price;
	EditText why, shuoming, num;
	Button tijiao;
	boolean ischeck;
	private OrderDetailBean orderDetailBean;
	LinearLayout goodsGroup;
	String reason,orderCode,uid,money,note;
	private OrderPresenter orderPresenter;
	private boolean isGetGoods;
	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_jintuikuan;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_title.setText("退款");
		uid=SPUtils.get(this, null, SpPublicParams.UID, "")+"";
		orderDetailBean=(OrderDetailBean) getIntent().getSerializableExtra("model");
		if(orderDetailBean!=null)
		orderCode=orderDetailBean.getOrderCode();
		for (int i = 0; i < orderDetailBean.getItems().size(); i++) {
				View view = LayoutInflater.from(ActivityTuiKuan.this)
						.inflate(R.layout.shouhou_goodsitem, null);
				XImageLoader.load(GlobalConfig.API_URL
						+ "/picture/index?id="
						+ orderDetailBean.getItems().get(i).getImg(),
						((ImageView) view.findViewById(R.id.goods_im)));
				((TextView) view.findViewById(R.id.goods_name))
						.setText(orderDetailBean.getItems().get(i)
								.getName());
				((TextView) view.findViewById(R.id.goods_price))
						.setText("￥"+orderDetailBean.getItems().get(i)
								.getPrice());
				goodsGroup.addView(view);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ischeck = true;
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.tijiao:
			reason=why.getText().toString();
			money=num.getText().toString();
			note=shuoming.getText().toString();
			orderPresenter=new OrderPresenter(this);
			orderPresenter.shenQingShouHou(uid, orderCode, money, reason, "1",isGetGoods,note);
			finish();
			break;
		case R.id.yishoudao:
			isGetGoods=true;
			yishoudao.setBackgroundResource(R.drawable.xuanzhong);
			meishoudao.setBackgroundResource(R.drawable.weixuanzhong);
			ischeck = !ischeck;
			break;
		case R.id.meishoudao:
			isGetGoods=false;
			meishoudao.setBackgroundResource(R.drawable.xuanzhong);
			yishoudao.setBackgroundResource(R.drawable.weixuanzhong);
			ischeck = !ischeck;
			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == OrderPresenter.ShenQingSuccess) {
				ByAlert.alert(entity.getData() + "");
				finish();
			} else if (entity.getEventType() == OrderPresenter.ShenQingFail) {
				ByAlert.alert(entity.getData() + "");
				// finish();
			}
		}
	}

}
